#ifndef __FBO_H__
#define __FBO_H__

#include "PortableGL.h"


namespace glutil
{
	/** Class used to store and manage Framebuffer Objects in OpenGL
	  @note This class was originally created (apparently) for the project in https://www.happy-toast.com/svn/trunk/hdrr-project/
	  There was no license information, though, so I guess it's suposed to be free.*/
	class FBO
	{
		public:
			/** Constructor*/
			FBO();
			/** Destructor */
			~FBO();

			/** Creates a render texture with the given properties. 
			  @param w Texture width
			  @param h Texture height
			  @param format Texture format. Examples: GL_RGBA, GL_RGBA16F_ARB, GL_RGBA32F_ARB
			 @param mipmaps ALWAYS LEAVE THIS AS FALSE
			 */
			void init(int w, int h, int format, bool mipmaps);

			/** Frees rendertextures memory*/
			void release();
		
			/** Gets the current texture */
			GLuint extractTexture();

			/** Binds the current texture in the currently active texture unit */
			void bind();

			/** Makes this FBO to be the rendertarget for all render commands from now on*/
			void beginCapture();
			/** Makes the backbuffer to be again the rendertarget */
			static void endCapture();

			/** Gets the texture width */
			int getWidth() const { return textureW; }

			/** Gets the texture height */
			int getHeight() const { return textureH; }

			/** Clears the texture contents */
			void clear();

		private:
			GLuint fb;
			GLuint depth_rb;
			GLuint color_tex;
			GLint internalformat;
			GLenum target;

			int textureW, textureH;
	};
}

#endif // ___FBO_H__
